<% layout('/layouts/default.html', {title: '单项指标', libs: ['layout','zTree','dataGrid']}){ %>
<div class="ui-layout-west">
    <div class="main-content">
        <div class="box box-main">
            <div class="box-header" style="padding-bottom: 4px;">
                <div class="box-title">
                    <i class="fa icon-grid"></i> ${text('考核得分')}
                </div>
            </div>
            <div class="box-body">
                <#form:form id="searchForm" action="${ctx}/exam/dcSupExamSumScoreResult/unitList" method="post" class="form-inline"
                data-page-no="${parameter.pageNo}" data-page-size="1000"
                data-order-by="${parameter.orderBy}">
                <#form:hidden path="examId" value="${examSumScore.examId}"/>
                <#form:hidden path="year" value="${examSumScore.year}"/>
                <div class="input-group">
                    <#form:input path="unitName" id="unitName" maxlength="200" class="form-control"
                     placeholder="请输入被考核单位名称" autocomplete="off"/>
                    <span class="input-group-addon" onclick="searchTaskDesc()" style="cursor: pointer">查询</span>
                    <span class="input-group-addon" onclick="cz();" style="cursor: pointer">重置</span>
                </div>
            </#form:form>
        </div>
        <div class="ui-layout-content">
            <table id="leftDataGrid"></table>
        </div>
    </div>
</div>
</div>
<div class="ui-layout-center">
    <iframe id="mainFrame" name="mainFrame" class="ui-layout-content p0"></iframe>
</div>
<% } %>
<style>
    #leftDataGrid tr{
        cursor: pointer;
    }
    .SelectBG td{
        background: #eaeaea !important;
        background-color: #eaeaea !important;
        color:blue;
    }


</style>
<script>
    // 初始化布局
    $('body').layout({
        west__initClosed: $(window).width() <= 767, // 是否默认关闭
        west__size: 200,
        onresize_end: function(){
            $('#leftDataGrid').dataGrid('resize');
        }
    });
    var win = $("#mainFrame")[0].contentWindow;
    // 定义全局变量
    var examSumScore = ${toJson(examSumScore)};

    $('#leftDataGrid').dataGrid({
        searchForm: $("#searchForm"),
        sortableColumn:false, // 禁止排序
        autoGridHeight: function(){
            var height = $(window).height()-170;
            return height;
        }, // 设置自动高度
        columnModel: [
            {header:'${text("考核单位code")}', name:'exameOrg',hidden:true, width:150, align:"left"},
            {header:'${text("被考核单位")}', name:'exameOrgName', width:150, align:"left",classes:"",
                formatter: function(val, obj, row, act){
                    if (row.scoreType == '9'){
                        return val +'<span style="color: dodgerblue">(已评分)</span>';
                    }else{
                        return val +'<span style="color: red">(未评分)</span>';
                    }
                }
            },
        ],
        emptyDataHint: true,
        altRows: false,
        // 选择行后调用（id:行id，isSelect：是否是选中，event：事件）
        onSelectRow: function(id, isSelect, event) {
            let selectRow = $('#leftDataGrid').dataGrid('getRowData', id);
            // requestInfo(examSumScore.examId,selectRow.exameOrg,examSumScore.year)
            $('#mainFrame').attr('src', '${ctx}/exam/dcSupExamSumScoreResult/resultCheckData?examId='+examSumScore.examId+'&orgCode='+selectRow.exameOrg+'&year='+examSumScore.year);
        },
        // 加载成功默认设置选择第一个
        ajaxSuccess: function(data){
            $('#leftDataGrid tr').eq(1).addClass('SelectBG')
            if (data.count>0){
                let row=data.list[0];
                $('#mainFrame').attr('src', '${ctx}/exam/dcSupExamSumScoreResult/resultCheckData?examId='+examSumScore.examId+'&orgCode='+row.exameOrg+'&year='+examSumScore.year);
            }
            $('#leftDataGrid tr').click(function(){
                $(this).siblings().removeClass('SelectBG');
                $(this).addClass('SelectBG');
            })
        }
    });
    function cz(){
        searchForm.reset();
        $('#leftDataGrid').dataGrid('reloadGrid');
    }


    function searchTaskDesc(){
        $("#searchForm").submit();
    }
    function requestInfo(examId,exameOrg,year){
        js.ajaxSubmit(
            '${ctx}/exam/dcSupExamSumScoreResult/resultCheckData?examId='+examId+'&orgCode='+exameOrg+'&year='+year ,
            function (data){
        });
    }

    // // 主页框架
    // $(function () {
    //     //查询被考核单位
    //     left(examSumScore.examId, null);
    // });
    //
    // // 输入框事件
    // function request() {
    //     var info = $("#unitName").val();
    //     if (info == ''){
    //         return;
    //     }
    //     left(examSumScore.examId,info);
    // }
    //
    // function left(examId, unitName) {
    //     js.ajaxSubmit({
    //         url: '${ctx}/exam/dcSupExamSumScoreResult/unitList',
    //         data: {'examId': examId, 'unitName': unitName},
    //         datatype: 'json',
    //         callback: function (res) {
    //             $("#leftDataGrid").dataGrid({
    //                 // 是否显示行号
    //                 showRownum: false,
    //                 data: res.page.list,
    //                 datatype: 'local',
    //                 columnModel: [
    //                     {
    //                         header: '${text("考核单位名称")}',
    //                         name: 'exameOrgName',
    //                         align: "center",
    //                         classes:""
    //                     },
    //                     {header: 'id', name: 'exameOrg', hidden: true} // 不可删除
    //                 ],
    //                 onSelectRow: function(id, isSelect, event) {
    //                     let selectRow = $('#leftDataGrid').dataGrid('getRowData', id);
    //                     $('#mainFrame').attr('src', '${ctx}/exam/dcSupExamSumScoreResult/resultCheckData?examId='+examSumScore.examId+'&orgCode='+selectRow.exameOrg+'&year='+examSumScore.year);
    //                 },
    //                 ajaxSuccess:function (data){
    //                     $('#leftDataGrid tr').eq(1).addClass('SelectBG')
    //                     if (data.total>0 && gobal_flag){
    //                         gobal_flag=false;
    //                         let row=data.rows[0];
    //                         $('#mainFrame').attr('src', '${ctx}/exam/dcSupExamSumScoreResult/resultCheckData?examId='+examSumScore.examId+'&orgCode='+row.exameOrg+'&year='+examSumScore.year);
    //                     }
    //                     $('#leftDataGrid tr').click(function(){
    //                         $(this).siblings().attr('class','ui-widget-content jqgrow ui-row-ltr');
    //                         $(this).attr('class','ui-widget-content jqgrow ui-row-ltr SelectBG');
    //                     })
    //                 }
    //             });
    //         },
    //         async: true,
    //         message: "正在获取数据.."
    //     })
    // }



    function exportFile(){
        // 传递参数有  考核任务id 考核年份  考核机构代码
        let allData = $("#leftDataGrid").dataGrid('getDataIDs');
        if (allData == ''){
            js.showErrorMessage("暂无数据导出");
            return;
        }
        var orgCode='';
        for (let code of allData){
            orgCode =orgCode +( $('#leftDataGrid').dataGrid('getRowData', code).exameOrg)+",";
        }
        let examId=examSumScore.examId;
        let year= examSumScore.year;
        let xhr = new XMLHttpRequest();
        xhr.open('post', '${ctx}/exam/dcSupExamSumScoreResult/export?examId='+examId+'&year='+year+'&orgCode='+orgCode, true);
        xhr.setRequestHeader('Content-Type', `application/json`);
        xhr.responseType = 'blob';
        let that = this;
        let fileName="自评查看";
        xhr.onload = function () {
            if (this.status == 200) {
                //接受二进制文件流
                let blob = this.response;
                let downloadElement = document.createElement('a');
                let href = blob;
                if (typeof blob == 'string') {
                    downloadElement.target = '_blank';
                } else {
                    href = window.URL.createObjectURL(blob) ;//创建下载的链接
                }
                downloadElement.href = href;
                //下载后文件名
                downloadElement.download = fileName +".xlsx";
                downloadElement.click(); //点击下载
                document.body.removeChild(downloadElement); //下载完成移除元素
                if (typeof blob != 'string') {
                    window.URL.revokeObjectURL(href); //释放掉blob对象
                }
            }
        }
        xhr.send();
    }


</script>
